package com.hxkj.service.userCenter.impl;

import com.hxkj.client.service.DefaultAvatarServiceClient;
import com.hxkj.domain.po.DefaultAvatar;
import com.hxkj.service.userCenter.DefaultAvatarService;
import com.platform.common.domain.exception.CustomMessageException;
import com.platform.common.domain.result.ResultDataStruct;
import com.platform.common.domain.result.ResultMessageStruct;
import com.platform.common.util.StringUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
@Slf4j
public class DefaultAvatarServiceImpl implements DefaultAvatarService {
    @Autowired
    private DefaultAvatarServiceClient defaultAvatarServiceClient;

    @Override
    public List<DefaultAvatar> getList() throws Exception {
         ResultDataStruct<List<DefaultAvatar>> result = defaultAvatarServiceClient.getList();
         if(result.getCode() != 200){
             throw new CustomMessageException(result.getMessage());
         }
         return result.getData();
    }

    @Override
    public void save(DefaultAvatar defaultAvatar) throws Exception {
        if(defaultAvatar == null){
            throw new CustomMessageException("不能保存空对象");
        }
        if(StringUtils.isBlank(defaultAvatar.getImgPath())){
            throw new CustomMessageException("文件路径不能为空");
        }
        defaultAvatar.setCreateTime(System.currentTimeMillis());
        defaultAvatar.setDelTag(0);
        defaultAvatar.setUpdateTime(System.currentTimeMillis());
        ResultMessageStruct result = defaultAvatarServiceClient.save(defaultAvatar);
        if(result.getCode() != 200){
            throw new CustomMessageException(result.getMessage());
        }
    }

    @Override
    public void delete(DefaultAvatar defaultAvatar) throws Exception {
        if(defaultAvatar == null){
            throw new CustomMessageException("对象为空");
        }
        if(defaultAvatar.getId() == null){
            throw new CustomMessageException("默认头像编号不能为空");
        }
        ResultMessageStruct result = defaultAvatarServiceClient.delete(defaultAvatar);
        if(result.getCode() != 200){
            throw new CustomMessageException(result.getMessage());
        }
    }

    @Override
    public DefaultAvatar getRandomInfo() throws Exception {
        ResultDataStruct<DefaultAvatar> result = defaultAvatarServiceClient.getRandomInfo();
        if(result.getCode() != 200){
            throw new CustomMessageException(result.getMessage());
        }
        return result.getData();
    }
}
